// pages/login/login.js
const util = require('../../utils/util.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    no: '',
    pwd: '',
    noinput: false,
    pwdinput: false,
    isLogin: false
  },
  noinput: function (e) {
    this.setData({ no: e.detail.value });
    this.setData({ noinput: true });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({ disabled: false });
    }

  },
  pwdinput: function (e) {
    this.setData({ pwd: e.detail.value });
    this.setData({ pwdinput: true });
    if (this.data.noinput == true && this.data.pwdinput == true) {
      this.setData({ disabled: false });
    }
  },
  layout: function(e) {
    console.log('注销')
    let that = this
    wx.showModal({
      title: '提示',
      content: '退出当前帐号',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '帐号退出成功',
          })
          wx.clearStorage()
          that.setData({
            isLogin: false
          })
        }
      }
    })
  },
  onGotUserInfo: function(e) {
    let that = this
    let username = that.data.no
    let password = that.data.pwd
    let userInfo = e.detail.userInfo

    if (username == '') {
      wx.showToast({
        title: '请输入帐号',
        icon: 'none',
        duration: 3000
      })
      return false
    }
    if (password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 3000
      })
      return false
    }
    wx.showLoading({
      title: '登录中...',
    })
    this.setData({ disabled: true });
    util.request('V1/operatorLogin',
      {
        // username: 'admin',
        // password:'admin2020@!',
        username: username,
        password: password
      },
      function (res) {
        console.log(res)
        wx.hideLoading()
        if (res.data.code == 200) {
          that.setData({
            avatarUrl: userInfo.avatarUrl,
            nickName: userInfo.nickName,
            isLogin: true,
            no: '',
            pwd: ''
          })

          util.setUserdata('userInfo', userInfo)
          let loginStatus = { isLogin: true }
          util.setUserdata('loginStatus', loginStatus)
          // wx.switchTab({
          //   url: '../order_list/order_list'
          // })
        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            success: function (result) {
              if (result.confirm) {
              }
            }
          })
        }
      },
      function (res) {
        //尝试重启
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          success: function (result) {
            if (result.confirm) {
              wx.reLaunch({
                url: '../login/login'
              })
            }
          }
        })
      },
      function (res) {
        // wx.hideLoading();
      }
    )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      disabled: false
    })
    
    let loginStatus = util.getUserdata('loginStatus')
    if (loginStatus.isLogin == true) {
      wx.switchTab({
        url: '../order_list/order_list'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    if (this.data.no == '' || this.data.pwd == '') {
      this.setData({ disabled: true });
    } else {
      this.setData({ disabled: false });
    }

    let userInfo = util.getUserdata('userInfo')
    let loginStatus = util.getUserdata('loginStatus')
    if (loginStatus && userInfo.avatarUrl) {
      that.setData({
        avatarUrl: userInfo.avatarUrl,
        nickName: userInfo.nickName,
        isLogin: true,
        no: '',
        pwd: ''
      })
    }else{
      that.setData({
        isLogin: false
      })
    }

    // let loginStatus = util.getUserdata('loginStatus')
    // if (loginStatus.isLogin == true) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '检测到帐号已登录,会自动切换到列表页',
    //     success: function(res) {
    //       wx.switchTab({
    //         url: '../order_list/order_list'
    //       })
    //     }
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})